package collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 集合练习
 */
public class Exercise02 {

    public static void main(String[] args) {
        /*
            使用 ArrayList 完成对 对象 Car{name,price}各种操作
            add:添加
            remove:删除
            contains:查找某个元素是否存在
            size:获取元素个数
            isEmpty:判断是否为空
            clear:清空
            addAll:添加多个元素
            containsAll:查找多个元素是否都存在
            removeAll:删除多个元素
            使用增强for和迭代器来遍历所有的 car，需要重写 Car 的 toString()
         */

        Car car = new Car("宝马", 400000);
        Car car2 = new Car("宾利", 5000000);

        List<Car> cars = new ArrayList<>();

        cars.add(car);
        cars.add(car2);
        cars.remove(car2);
        boolean contains = cars.contains(car2);
        System.out.println(contains);
        boolean empty = cars.isEmpty();
        System.out.println(empty);
        cars.clear();
        System.out.println(cars);
        List<Car> temp = new ArrayList<>();
        temp.add(car);
        temp.add(car2);
        cars.addAll(temp);
        System.out.println(cars);
        cars.containsAll(temp);
        cars.removeAll(temp);
        System.out.println(cars);

        cars.add(car);
        cars.add(car2);

        System.out.println("====使用增强 for 完成遍历====");
        for (Car car1 : cars) {
            System.out.println(car1);
        }

        System.out.println("====使用迭代器完成遍历====");
        Iterator<Car> iterator = cars.iterator();
        while (iterator.hasNext()) {
            Car obj = iterator.next();
            System.out.println(obj);
        }
    }
}
